
if (project.properties.downloadFreemind == 'true') {
    def file = downloadFeed();
    def url = findZipRelease(file);
    def freemind = downloadRelease(url);
    package_plugin(freemind);
}

if (project.properties.packageFreemind == 'true') {
    def freemind = project.properties.localFreemindZipFile;
    println ("using local zip: " + freemind)
    package_plugin(freemind);
}

def downloadFeed() {
    ant.get(src:'http://sourceforge.net/projects/freemind/feed?filter=file',dest:'target/sample_feed.html')
    return new File('target/sample_feed.html');
}


def findZipRelease(file) {
    def fullurl = null;
    file.eachLine {
        if (it =~ 'href' && it =~'freemind-bin-max' ) {
            def matcher = (it  =~ /File released: (.*)<\/a>/);
            def path = matcher[0][1];
            println("Path: " + path);
            if (fullurl == null) {
                fullurl = 'http://downloads.sourceforge.net/project/freemind' + path + '?use_mirror=cdnetworks-us-2';
            }
        }
    }
    return fullurl;
}

def downloadRelease(url) {
    def dest = 'target/freemind.zip';
    ant.get(src:url,dest:dest)
    return dest;
}


def package_plugin(src_freemind_zip) {
    ant.unzip(src:src_freemind_zip,dest:'target/freemind-zip');
}

